package cn.supermarket.config;


import cn.supermarket.bean.AliPayBean;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;


@Data
@Configuration
public class AliPayConfiguration {


    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(AliPayConfiguration.class);

    private final String format = "json";

    /**
     * appId
     */
    @Value("2021000121604160")
    private String appId;

    /**
     * 商户私钥
     */
    @Value("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCVf4kPurBvPYHXz6vawsxtcNpLlDTx5vLlDkPSsDNtMoSTMzoPoMx5uR+D87bRTfgmDlIJeGBSiTqrXVUDZSPbvtRFWUDr7Xh2oyMsYkYWK/JwmehYQmd3nBJLsErGdEtPpMASYIB4U3H/4B1TGHKGf9zgG+iBcNvaznzjFrphGm624jbZs0Q2j0bKBhpfbDrjyeUDCvD6AjsHbbgaKXkFeqYUsj9QCccRtR9Z0JMe4qmcQi7XUvurIBelMWrq/R6+h68Cv/8b0IFidhMbHHn8x4ZM9zvlE7BoMC0u7+o7mGGoGtUn3QxWlSiRFnTXWsV7bx761U3m6jF6H58AiZd/AgMBAAECggEAfVPOdmY+lPMOT/qkCp/1uuW5y1Ry0jyXcdj0mMLoSeno5HI0gXd/E6hTOE5u+6aOmnC064WD0o2Cprh2lYMLfaPL1ltNpruMmoCG8yVrthifhTAxbRQ2Izqux88B0L8bh6gI8R7/dGAYGLcsPkYndIPqieI7l5Aib5tdbQ7XrqCrq/HfCgZl4r7/01f8sbmEAO5YU4aCCOVqK42JlrQZJGqbklOBlG7gP/+5giQ9oMJ6Gg7j4IQDKh5fZrJ3p+tKDIC5RFsmHTNSc+qm6/r7Zn/LfrUa1swBGHL6n8nhF+G1qTB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5SCXhgUok6q11VA2Uj277URVlA6+14dqMjLGJGFivycJnoWEJnd5wSS7BKxnRLT6TAEmCAeFNx/+AdUxhyhn/c4BvogXDb2s584xa6YRputuI22bNENo9GygYaX2w648nlAwrw+gI7B224Gil5BXqmFLI/UAnHEbUfWdCTHuKpnEIu11L7qyAXpTFq6v0evoevAr//G9CBYnYTGxx5/MeGTPc75ROwaDAtLu/qO5hhqBrVJ90MVpUokRZ011rFe28e+tVN5uoxeh+fAImXfwIDAQAB")
    private String publicKey;

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    @Value("http://localhost:8080/static/error_url.html")
    private String notifyUrl;

    /**
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:8080/static/return_url.html")
    private String returnUrl;

    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;

    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;

    /**
     * 支付宝网关
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;

    public String pay(AliPayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }

}
